#ifndef SERIAL_H
#define SERIAL_H

#include <QObject>
#include <QUdpSocket>
#include <QtSerialPort/QSerialPort>
#include <QTimer>

QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
QT_FORWARD_DECLARE_CLASS(QWebSocket)

class serial : public QObject
{
    Q_OBJECT

public:
    serial(QSerialPort *serialPort, QObject *parent = 0);
    ~serial();

private:
    QSerialPort *m_serialPort;
    QUdpSocket *m_rev_soc;    //接收数据
    QUdpSocket *m_send_soc;   //发送数据
    QUdpSocket *m_control_soc;//接收控制命令

    QWebSocketServer *m_pWebSocketServer;
    QList<QWebSocket *> m_clients;
    qint16 port;

    QByteArray brocast_byte;

    void handleCmd(const QByteArray & cmds);
    void refreshPath(const QByteArray & path);


private slots:
    void handleSocReadyRead();
    void handleSerialReadyRead();
    void handleCmdReadyRead();
    void handleError(QSerialPort::SerialPortError error);

    void onNewConnection();
    void processMessage(const QString &message);
    void socketDisconnected();

    void heartbeat();
};

#endif // SERIAL_H
